﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Seasar.Framework.Aop.Interceptors;
using Shsc.Net.FrameWork.Common.Log;
using Shsc.Net.FrameWork.Common.SysException;

namespace Shsc.Net.FrameWork.Common.Interceptor
{
    /// <summary>
    /// 异常处理
    /// </summary>
    public class ExceptionInterceptor : AbstractInterceptor
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="invocation"></param>
        /// <returns></returns>
        public override object Invoke(Seasar.Framework.Aop.IMethodInvocation invocation)
        {
            lock (this)
            {
                Log4NetLogger logger = Log4NetLogger.CreateLogger(invocation.Target.GetType());

                object ret = null;

                try
                {
                    ret = invocation.Proceed();
                }
                catch (BusinessException bex)
                {
                    logger.Error(String.Empty, bex);
                    throw bex;
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    logger.Error(String.Empty, ex);
                    throw new DBAccessException();
                }
                catch (System.Data.OleDb.OleDbException ex)
                {
                    logger.Error(String.Empty, ex);
                    throw new DBAccessException();
                }
                catch (System.Data.OracleClient.OracleException ex)
                {
                    logger.Error(String.Empty, ex);
                    throw new DBAccessException();
                }
                catch (Exception ex)
                {
                    logger.Fatal(ex);
                    throw ex;
                }
            }
            return base.Invoke(invocation);
        }
    }
}
